/**
* Copyright 2009, Jethro Grassie, Creative Jar Technology Ltd (http://www.cjtech.co.uk)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
**/

package com.adobe.cairngorm.business;

import java.lang.IllegalStateException;
import javafx.io.http.HttpRequest;

public class HTTPService extends AbstractService
{
    public var makeRequest:function():HttpRequest;

    public function start():Void
    {
        if(makeRequest == null)
        {
            throw new IllegalStateException("No makeRequest defined.");
        }
        var request:HttpRequest = makeRequest();
        if(request == null)
        {
            throw new IllegalStateException("No HttpRequest returned from makeRequest.");
        }
        request.onInput = function(data:Object):Void
        {
            if(responders.size() > 0)
            {
                var lastResponder:Responder = responders.get(responders.size()-1) as Responder;
                lastResponder.onResult(data);
                responders.remove(lastResponder);
            }
        }
        request.onError = function(data:Object):Void
        {
            if(responders.size() > 0)
            {
                var lastResponder:Responder = responders.get(responders.size()-1) as Responder;
                lastResponder.onFault(data);
                responders.remove(lastResponder);
            }
        }
        request.start();
    }
}

